/*
 *  Copyright 2010 BeTo.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

/*
 * ChangeAlbumDialog.java
 *
 * Created on 04-mar-2010, 17:25:09
 */
package edu.upf.userinterface.impl;

import edu.upf.content.DescriptionContent;
import edu.upf.content.ImageObject;
import edu.upf.utils.Definitions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import javax.swing.ImageIcon;

/**
 *
 * @author BeTo
 */
public class ChangeAlbumDialog extends javax.swing.JDialog implements Definitions {

    private DescriptionContent selected;
    private File sharedfolder = new File("Shared_Folder");
    private File originalmedia;
    private File copiedmedia;
    private String album = null;
    private GraphicInterface parent;
    private boolean change = false;

    /** 
     * Creates new form ChangeAlbumDialog
     * @param parent
     * @param modal
     * @param file
     */
    public ChangeAlbumDialog(java.awt.Frame parent, boolean modal, DescriptionContent file) {
        super(parent, modal);
        this.parent = (GraphicInterface) parent;
        initComponents();
        this.selected = file;
        nameLabel.setText(selected.getName());
        ImageIcon icon = null;
        if (file instanceof ImageObject) {
            ((ImageObject) file).createThumbnail(LIB_THUMB_H, LIB_THUMB_W);
            icon = file.getThumbnail();
        }
        imageLabel.setIcon(icon);
        updateDestAlbumView();
        this.setLocationByPlatform(true);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        destAlbumText = new javax.swing.JComboBox();
        newAlbumButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        acceptButton = new javax.swing.JButton();
        imageLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change Album");

        jLabel1.setText("The selected file:");

        nameLabel.setForeground(new java.awt.Color(255, 0, 0));

        newAlbumButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button_icons/new_album.png"))); // NOI18N
        newAlbumButton.setText("New Album");
        newAlbumButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newAlbumButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Select destiny album:");

        jLabel3.setText("will be moved.");

        acceptButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/button_icons/accept20px.png"))); // NOI18N
        acceptButton.setText("Accept");
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        imageLabel.setMaximumSize(new java.awt.Dimension(140, 140));
        imageLabel.setMinimumSize(new java.awt.Dimension(140, 140));
        imageLabel.setPreferredSize(new java.awt.Dimension(140, 140));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                                .add(nameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 236, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 91, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(imageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .add(layout.createSequentialGroup()
                            .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .add(267, 267, 267))
                        .add(layout.createSequentialGroup()
                            .add(destAlbumText, 0, 260, Short.MAX_VALUE)
                            .add(18, 18, 18)
                            .add(newAlbumButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(23, 23, 23)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(acceptButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(149, 149, 149))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(31, 31, 31)
                        .add(nameLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(imageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(destAlbumText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(newAlbumButton))
                .add(18, 18, 18)
                .add(acceptButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 32, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newAlbumButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newAlbumButtonActionPerformed
        NewAlbumDialog newAlbum = new NewAlbumDialog(new javax.swing.JFrame(), true);
        String newalbumname = newAlbum.getNewAlbumName();
        if (newalbumname != null) {
            File newAlb = new File("Shared_Folder/" + newalbumname);
            newAlb.mkdir();
            updateDestAlbumView();
        }
}//GEN-LAST:event_newAlbumButtonActionPerformed

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed

        String path = selected.getPath();
        String originalname;
        try {
            originalmedia = new File(path);
            originalname = originalmedia.getName();
            destAlbumText.getSelectedItem();
            album = (String) destAlbumText.getSelectedItem();
            copiedmedia = new File("Shared_Folder/" + album + "/" + originalname);
            FileChannel inChannel = new FileInputStream(originalmedia).getChannel();
            FileChannel outChannel = new FileOutputStream(copiedmedia).getChannel();
            try {
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } catch (IOException ex) {
                parent.showExceptionMessage("Error", ex.getMessage(), getClass().getName());
            } finally {
                try {
                    if (inChannel != null) {
                        inChannel.close();
                    }
                    if (outChannel != null) {
                        outChannel.close();
                    }
                } catch (IOException ex) {
                    parent.showExceptionMessage("Error", ex.getMessage(), getClass().getName());
                }
            }
        } catch (FileNotFoundException ex) {
            parent.showExceptionMessage("Error", ex.getMessage(), getClass().getName());
        }
        if (!originalmedia.delete()) {
            parent.showExceptionMessage("Error", "Fail deleting original file.", getClass().getName());
        }
        change = true;
        this.dispose();
}//GEN-LAST:event_acceptButtonActionPerformed

    public String getNewAlbum() {
        return album;
    }

    public boolean Change() {
        return change;
    }

    public void updateDestAlbumView() {
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return !name.startsWith(".");
            }
        };
        String[] children = sharedfolder.list(filter);
        destAlbumText.setModel(new ComboBoxModel(children));
        destAlbumText.setSelectedIndex(0);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JComboBox destAlbumText;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton newAlbumButton;
    // End of variables declaration//GEN-END:variables
}
